package com.wc.codeforces.二进制.Alya_and_Permutation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/28 15:44
 * @description https://codeforces.com/contest/2035/problem/C
 */
public class Main {
    /**
     * 思路：找规律<p>
     * 偶数的大满贯 二进制位上全是1, 当最后一位是2^cnt - 1, 那就把 1 2对调就好<p>
     * 如果不是, 那就把 2^(cnt - 1) - 1放最后, 身下的直接按照顺序排列就好<p>
     * 奇数的话, 就是 n - 1的大满贯 + 最后一位 1<p>
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            int x, maxn;

            if ((n & 1) == 1) {
                maxn = x = n - 1;
            } else maxn = x = n;

            int cnt = 0, len = 0;
            while (x > 0) {
                len++;
                cnt += x & 1;
                x >>= 1;
            }
            int last = 0;
            for (int i = 0; i < len - 1; i++) {
                last += 1 << i;
            }
            if ((n & 1) == 1) out.println(n);
            else out.println(last + (1 << len - 1));
            if (cnt == 1) {
                for (int i = 1; i <= maxn; i++) {
                    if (i == 1) out.print(2 + " ");
                    else if (i == 2) out.print(1 + " ");
                    else out.print(i + " ");
                }
            } else {
                for (int i = 1; i <= maxn; i++) {
                    if (i == last) continue;
                    out.print(i + " ");
                }
                out.print(last + " ");
            }
            if ((n & 1) == 1) out.print(n);
            out.println();
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
